package com.example.myyunpan.Controller;

import com.example.myyunpan.annotation.GlobalIntercepter;
import com.example.myyunpan.annotation.VerifyParam;
import com.example.myyunpan.entity.constants.Constants;
import com.example.myyunpan.entity.domain.FileShare;
import com.example.myyunpan.entity.dto.SessionWebUserDto;
import com.example.myyunpan.entity.enums.FileDelFlagEnums;
import com.example.myyunpan.entity.query.FileInfoQuery;
import com.example.myyunpan.entity.query.FileShareQuery;
import com.example.myyunpan.entity.vo.FileInfoVO;
import com.example.myyunpan.entity.vo.PaginationResultVO;
import com.example.myyunpan.entity.vo.ResponseVO;
import com.example.myyunpan.service.FileShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/share")
public class ShareController {

    @Autowired
    private FileShareService fileShareService;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadShareList")
    @GlobalIntercepter(checkParams = true)
    public ResponseVO loadShareList(HttpSession session, FileShareQuery query){
//        TODO 报错有问题
        query.setOrderBy("share_time desc");
        SessionWebUserDto userDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        query.setUserId(userDto.getUserId());
        query.setQueryFileName(true);
        PaginationResultVO resultVO = fileShareService.findListByPage(query);
        return ResponseVO.success(resultVO);
    }


    /**
     * 分享文件
     * @param session
     * @param fileId
     * @param validType
     * @return
     */
    @RequestMapping("/shareFile")
    @GlobalIntercepter(checkParams = true)
    public ResponseVO shareFile(HttpSession session,
                                @VerifyParam(required = true) String fileId,
                                @VerifyParam(required = true) Integer validType,
                                String code){
        SessionWebUserDto userDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        FileShare share = new FileShare();
        share.setFileId(fileId);
        share.setValidType(validType);
        share.setCode(code);
        share.setUserId(userDto.getUserId());
        fileShareService.saveShare(share);
        return ResponseVO.success(share);
    }


    @RequestMapping("/cancelShare")
    @GlobalIntercepter(checkParams = true)
    public ResponseVO cancelShare(HttpSession session,
                                @VerifyParam(required = true) String shareIds){
        SessionWebUserDto userDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        fileShareService.deleteFileShareBatch(userDto.getUserId(),shareIds.split(","));
        return ResponseVO.success();
    }

}
